<!-- 库存管理-商品调拨管理 -->
<template>
  <div class="mod-config">
    <el-button-group style="margin-bottom: 10px;">
      <el-button type="primary" icon="el-icon-search" @click="searchHandle">查询</el-button>
      <el-button type="primary" icon="el-icon-delete" @click="searchReset">清空</el-button>
      <el-button v-if="permissions.eims_allocation_add && this.deptType ==3" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle(0)">新增</el-button>
      <el-button v-if="permissions.eims_allocation_add && this.deptType !=3" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle(1)">退货新增</el-button>
    </el-button-group>
    <el-form inline :model="dataForm" label-width="100px" @keyup.enter.native="searchHandle">
      <el-form-item label="调拨单号">
        <el-input v-model="dataForm.billId"></el-input>
      </el-form-item>
      <el-form-item label="发出部门">
        <el-select v-model="dataForm.outDeptId" clearable>
          <el-option v-for="item in deptOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="接收部门">
        <el-select v-model="dataForm.inDeptId" clearable>
          <el-option v-for="item in deptOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品名称">
        <el-input v-model="dataForm.viewGoodsName"></el-input>
      </el-form-item>
      <el-form-item label="单据日期">
        <el-row>
          <el-col :span="11">
            <el-date-picker v-model="dataForm.createTimeStart" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
          </el-col>
          <el-col class="line" :span="2" style="text-align: center;">-</el-col>
          <el-col :span="11">
            <el-date-picker v-model="dataForm.createTimeEnd" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="商品代码">
        <el-input v-model="dataForm.goodsCode"></el-input>
      </el-form-item>
      <el-form-item label="审核状态">
        <el-select v-model="dataForm.allocationStatus" clearable>
          <el-option label="未审核" value="1"></el-option>
          <el-option label="已审核" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="收货状态">
        <el-select v-model="dataForm.receiveState" clearable>
          <el-option label="未收货" value="0"></el-option>
          <el-option label="已收货" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品类别">
        <el-select v-model="dataForm.ctgId" @change="changeCtgId" clearable>
          <el-option v-for="item in ctgOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="制造商">
        <el-input v-model="dataForm.supplyName" @change="changeSupplyId">
          <el-button slot="append" @click="openSupplyDialog">选择</el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="商品品种" prop="brandName">
        <el-input v-model="dataForm.brandName" readonly="readonly">
          <el-button slot="append" @click="getBrandList">选择</el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="制单人">
        <el-input v-model="dataForm.createUserName"></el-input>
      </el-form-item>
      <el-form-item label="收货人">
        <el-input v-model="dataForm.receiveUserName"></el-input>
      </el-form-item>
      <el-form-item label="审核人">
        <el-input v-model="dataForm.auditUserName"></el-input>
      </el-form-item>
      <el-form-item label="关联单号">
        <el-input v-model="dataForm.linkBillId"></el-input>
      </el-form-item>
      <el-form-item label="调拨类型">
        <el-select v-model="dataForm.allocationType" clearable>
          <el-option value="2" label="正常调拨"></el-option>
          <el-option value="1" label="自动调拨"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="打印状态">
        <el-select v-model="dataForm.receiveState" clearable>
          <el-option label="已打印" value="1"></el-option>
          <el-option label="未打印" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="dataForm.memo"></el-input>
      </el-form-item>
      <el-row>
        <el-form-item label="是否显示商品采购数量" label-width="175px">
          <el-checkbox v-model="dataForm.countShowFlag">显示</el-checkbox>
        </el-form-item>
        <el-form-item label="出库仓位" v-if="this.dataForm.countShowFlag">
          <el-select v-model="dataForm.outStockId" clearable>
            <el-option v-for="item in stockOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入库仓位" v-if="this.dataForm.countShowFlag">
          <el-select v-model="dataForm.inStockId" clearable>
            <el-option v-for="item in stockOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-row>
      <div v-if="this.dataForm.countShowFlag">
        <el-row class="bg-row">
          调拨调出商品数量总计：{{ this.outSum }}
        </el-row>
        <el-row class="bg-row">
          调拨收货商品数量总计：{{ this.inSum }}
        </el-row>
      </div>
    </el-form>
    <div class="avue-crud">
      <el-table :data="dataList" border v-loading="dataListLoading" :max-height="tableHeight">
        <el-table-column type="selection" width="40" style="text-align: center;"></el-table-column>
        <el-table-column label="操作" width="230" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-download" @click="receiveHandle(scope.row.id,'detail')">详情</el-button>
            <el-button :key="'edit' + scope.$index" v-if="permissions.eims_allocation_edit && scope.row.allocationStatus == '1' && (userInfo.lessDeptId == scope.row.outDeptId || userInfo.lessDeptId == '1')" type="text" icon="el-icon-edit" @click="addOrUpdateHandle('',scope.row.id)">修改</el-button>
            <el-button :key="'edit2' + scope.$index" v-else-if="permissions.eims_allocation_edit" type="text" disabled icon="el-icon-edit">修改</el-button>
            <el-button :key="'receive' + scope.$index" v-if="permissions.eims_allocation_receive && scope.row.allocationStatus == '2' && userInfo.lessDeptId == scope.row.inDeptId"
              type="text" icon="el-icon-download" @click="receiveHandle(scope.row.id,'receive')">收货</el-button>
            <el-button :key="'receive2' + scope.$index" v-else-if="permissions.eims_allocation_receive" disabled type="text" icon="el-icon-download">收货</el-button>

            <el-button :key="'del' + scope.$index" v-if="permissions.eims_allocation_del && (scope.row.allocationStatus == '3' || (userInfo.lessDeptId == scope.row.inDeptId && userInfo.lessDeptId != '1'))"
              type="text" disabled icon="el-icon-delete">删除</el-button>
            <el-button :key="'del2' + scope.$index" v-else-if="permissions.eims_allocation_del" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)"
              v-prevent-re-click>删除</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="billId" label="调拨单号" width="140"></el-table-column>
        <el-table-column prop="outDeptName" label="发出部门" width="110"></el-table-column>
        <el-table-column prop="inDeptName" label="接收部门" width="110"></el-table-column>
        <el-table-column prop="createUserName" label="制单人" width="60"></el-table-column>
        <el-table-column prop="createTime" label="制单日期" width="100">
          <template slot-scope="scope">
            {{dateFormat(scope.row.createTime)}}
          </template>
        </el-table-column>
        <el-table-column prop="auditUserName" label="审核人" width="60"></el-table-column>
        <el-table-column prop="auditTime" label="审核日期" width="100">
          <template slot-scope="scope">
            {{dateFormat(scope.row.auditTime)}}
          </template>
        </el-table-column>
        <el-table-column prop="receiveUserName" label="收货人" width="60"></el-table-column>
        <el-table-column prop="receiveTime" label="收货日期" width="100">
          <template slot-scope="scope">
            {{dateFormat(scope.row.receiveTime)}}
          </template>
        </el-table-column>
        <el-table-column prop="memo" label="备注" show-overflow-tooltip></el-table-column>
      </el-table>
    </div>
    <div class="avue-crud__pagination" style="margin-bottom: 25px;">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100, 200]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next">
      </el-pagination>
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
    <!-- 弹窗，收货 -->
    <receive-form v-if="receiveVisible" ref="receiveForm" @refreshDataList="getDataList"></receive-form>
    <!-- 弹窗，查询制造商 -->
    <search-supply v-if="supplyVisible" ref="supplyDialog" @refreshData="getSupplyData"></search-supply>
    <!-- 弹窗，查询商品品种 -->
    <select-brand v-if="selectBrandVisible" ref="selectBrand" @refreshData="updateFormBrand"></select-brand>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { filterParams } from '@/util/tools'

  import {
    allocationFetchList,
    allocationDel,
    getGoodsQuantitySum
  } from '@/api/eims/allocation'
  import { fetchAddFeeList, delAddFeeObj } from '@/api/eims/add-fee'
  import { getDeptInfo, getObj } from '@/api/admin/dept'
  import { getCtgList } from '@/api/eims/ctg'
  import { getStockList } from '@/api/eims/stock'

  import TableForm from './form'
  import ReceiveForm from './receive'
  import SearchSupply from '@/components/business/SearchSupply'
  import SelectBrand from '../storage/SearchBrand'

  export default {
    data() {
      return {
        searchFormShow: true,
        deptType: '',
        dataForm: {
          allocationType: '',
          supplyId: '',
          supplyName: '',
          brandId: '',
          brandName: '',
          outStockId: '',
          inStockId: ''
        },
        deptOptions: [],
        deptOptions1: [],
        dataList: [],
        pageIndex: 1,
        pageSize: 200,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        receiveVisible: false,
        ctgOptions: [],
        supplyVisible: false,
        selectBrandVisible: false,
        stockOptions: [],
        inSum: '',
        outSum: ''
      }
    },
    components: {
      TableForm,
      ReceiveForm,
      SearchSupply,
      SelectBrand
    },
    created() {
      this.getDeptList();
      this.getGoodsCtgList();
      this.getStock();
      this.setDeptId();
      this.getDeptType();
    },
    computed: {
      ...mapGetters(['permissions', 'userInfo']),
      tableHeight: function(){
        return window.innerHeight - 260
      }
    },
    methods: {
      //  查询数据
      searchHandle() {
        this.pageIndex = 1
        this.getDataList()
      },
      //  清空搜索表单
      searchReset() {
        for (let key in this.dataForm) {
          if (key == 'allocationType') {
            this.dataForm[key] = '2'
          } else {
            this.dataForm[key] = ''
          }
        }
        this.inSum = '',
          this.outSum = ''
      },
      //若用户不为管理员，进入页面后初始化获取当前部门
      setDeptId() {
        //this.$set(this.dataForm,'inDeptId',this.userInfo.lessDeptId)
        //this.$set(this.dataForm,'outDeptId',this.userInfo.lessDeptId)
        this.getDataList()
      },
      getDeptType() {
        getObj(this.userInfo.lessDeptId).then(response => {
					if(response.data.code == '0'){
						this.deptType = response.data.data.deptType
					}else{
						this.$message.error(response.data.msg)
					}
        })
      },
      // 获取数据列表
      getDataList() {
        let params1 = {
          current: this.pageIndex,
          size: this.pageSize
        }
        let params2 = this.filterParams(this.dataForm)
        this.dataListLoading = true

        allocationFetchList(Object.assign(params1, params2)).then(response => {
          if(response.data.code == '0'){
            this.$nextTick(() => {
              this.dataList = response.data.data.records
              this.totalPage = response.data.data.total
              this.dataListLoading = false
            })
          }else{
            this.$message.error(response.data.msg)
          }
        })
        if (this.dataForm.countShowFlag) {
          if (this.dataForm.outStockId == '' && this.dataForm.inStockId == '') {
            getGoodsQuantitySum(params2).then(response => {
              if(response.data.code == '0'){
                this.outSum = response.data.data
                this.inSum = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
          //调拨调出商品数量总计
          if (this.dataForm.outStockId != '') {
            params2.inOrOutFlag = "false"
            getGoodsQuantitySum(params2).then(response => {
              if(response.data.code == '0'){
                this.outSum = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
          //调拨收入商品数量总计
          if (this.dataForm.inStockId != '') {
            this.dataForm.inOrOutFlag = "true"
            getGoodsQuantitySum(this.dataForm).then(response => {
              if(response.data.code == '0'){
                this.inSum = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        }
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      addOrUpdateHandle(type, id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(type, id)
        })
      },
      // 删除
      deleteHandle(id) {
        this.$confirm('是否确认删除ID为' + id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return allocationDel(id)
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success('删除成功')
          }else{
            this.$message.error(response.data.msg)
          }
          this.getDataList()
        })
      },
      //  收货
      receiveHandle(id, type) {
        this.receiveVisible = true
        this.$nextTick(() => {
          this.$refs.receiveForm.init(id, type)
        })
      },
      //  获取部门列表
      getDeptList() {
        if (this.userInfo.lessDeptId == '1') {
          getDeptInfo().then(response => {
            if(response.data.code == '0'){
              this.deptOptions = response.data.data
            }else{
              this.$message.error(response.data.msg)
            }
          })
        } else {
          this.deptOptions = [{
            id: this.userInfo.lessDeptId,
            name: this.userInfo.lessDeptName
          }]
        }
      },
      //  获取商品类别
      getGoodsCtgList() {
        getCtgList().then(response => {
          if(response.data.code == '0'){
            this.ctgOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      changeCtgId() {
        this.dataForm.supplyId = ''
        this.dataForm.supplyName = ''
        this.dataForm.brandId = ''
        this.dataForm.brandName = ''
      },
      //  打开选择制造商弹窗
      openSupplyDialog() {
        let data = {
          ctgId: this.dataForm.ctgId
        }
        this.supplyVisible = true
        this.$nextTick(() => {
          this.$refs.supplyDialog.init(data)
        })
      },
      getSupplyData(data) {
        this.$set(this.dataForm, 'supplyId', data.id)
        this.$set(this.dataForm, 'supplyName', data.name)
      },
      changeSupplyId() {
        this.dataForm.brandId = ''
        this.dataForm.brandName = ''
      },
      // 获取商品品种信息
      getBrandList() {
        if (!this.dataForm.ctgId) {
          this.$message.warning('请选择商品类别')
          return false
        }
        if (!this.dataForm.supplyId) {
          this.$message.warning('请选择制造商')
          return false
        }
        let data = {
          ctgId: this.dataForm.ctgId,
          supplyId: this.dataForm.supplyId
        }
        this.selectBrandVisible = true
        this.$nextTick(() => {
          this.$refs.selectBrand.init(data)
        })
      },
      //  更新表单品种信息
      updateFormBrand(data) {
        this.dataForm.brandId = data.id
        this.dataForm.brandName = data.name
        this.dataForm.brandCode = data.code
        this.dataForm.name = data.name
        this.dataForm.unitType = data.unitType
      },
      //  获取仓位列表
      getStock() {
        getStockList().then(response => {
          if(response.data.code == '0'){
            this.stockOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      //  日期格式
      dateFormat(dateStr) {
        if (dateStr) {
          return dateStr.substring(0, 16)
        }

      }
    }
  }
</script>
